package lock;

import java.util.concurrent.Semaphore;
import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReentrantLock;

public class ReenreantLockTest {
    static Lock lock = new ReentrantLock();
    public static void main(String[] args) {
        Semaphore s = new Semaphore(5);  // 信号量，控制并发量

        for (int i = 0; i < 10; i++) {
            new Thread(()->{
                try {
                    System.out.println(s.availablePermits());
                    s.acquire();
                    Thread.sleep(3000);
                    System.out.println(Thread.currentThread().getName() + "get..");
                }
                catch (InterruptedException e) {
                    e.printStackTrace();
                }
                System.out.println(Thread.currentThread().getName() + "release..");
                s.release();
            }).start();
        }
    }

    public static void fun(int i) {
        lock.lock();
        if (i <= 0) {
            lock.unlock();
            return;
        }
        System.out.println(i);

        fun(i - 1);
    }
}
